package com.atguigu.flink.chapter11;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/10/20 15:17
 */
public class Flink11_Window_Over_1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        DataStream<WaterSensor> stream =
            env
                .fromElements(new WaterSensor("sensor_1", 1000L, 10),
                              new WaterSensor("sensor_1", 4000L, 20),
                              new WaterSensor("sensor_1", 4000L, 40),
                              new WaterSensor("sensor_1", 5000L, 50),
                              new WaterSensor("sensor_2", 5001L, 30),
                              new WaterSensor("sensor_2", 6000L, 60))
                .assignTimestampsAndWatermarks(
                    WatermarkStrategy
                        .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((ws, ts) -> ws.getTs())
                );
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        // sum(..) over(partition by id order by ts rows between unbounded preceding and current row)
        Table table = tenv.fromDataStream(stream, $("id"), $("ts").rowtime(), $("vc"));
        tenv.createTemporaryView("sensor", table);
        
        
        /*tenv.sqlQuery("select" +
                          " id, " +
                          " ts," +
                          " vc," +
//                          " sum(vc) over(partition by id order by ts rows between unbounded preceding and current row) vc_sum " +
//                          " sum(vc) over(partition by id order by ts rows between 1 preceding and current row) vc_sum " +
//                          " sum(vc) over(partition by id order by ts range between unbounded preceding and current row) vc_sum " +
//                          " sum(vc) over(partition by id order by ts range between interval '1' second preceding and current row) vc_sum " +
                          " sum(vc) over(partition by id order by ts) vc_sum " +
                          "from sensor")
            .execute()
            .print();*/
        
        tenv.sqlQuery("select" +
                          " id, " +
                          " ts," +
                          " vc," +
                          " sum(vc) over w vc_sum, " +
                          " max(vc) over w vc_max, " +
                          " min(vc) over w vc_min " +
                          "from default_catalog.default_database.sensor " +
                          "window w as(partition by id order by ts rows between unbounded preceding and current row)")
            .execute()
            .print();
        
    }
}
